<template>
	<view class="refund-page">
		<!-- 表单内容 -->
		<scroll-view scroll-y class="form-container" :style="{height: scrollHeight + 'px'}">
			<!-- 商品信息 -->
			<view class="section">
				<view class="section-title">退款商品</view>
				<view class="goods-card">
					<u-image :src="goodsInfo.goodsImage" width="120rpx" height="120rpx" radius="8"></u-image>
					<view class="goods-info">
						<view class="goods-name">{{goodsInfo.goodsName}}</view>
						<view class="goods-spec">规格：{{goodsInfo.spec}}</view>
						<view class="goods-price">¥{{goodsInfo.price}}</view>
					</view>
				</view>
			</view>

			<!-- 退款类型 -->
			<view class="section">
				<view class="section-title">退款类型</view>
				<u-radio-group v-model="refundType">
					<u-radio style="padding: 10rpx;" v-for="item in refundTypes" :key="item.value" :name="item.value"
						:label="item.label" activeColor="#ff2d51"></u-radio>
				</u-radio-group>
			</view>

			<!-- 退款原因 -->
			<view class="section">
				<view class="section-title">退款原因</view>
				<u-cell-group>
					<u-cell @click="showReasonPicker = true" :title="refundReason" isLink></u-cell>
				</u-cell-group>
				<u-picker :show="showReasonPicker" :columns="reasonColumns" keyName="label" @confirm="reasonConfirm"
					@cancel="showReasonPicker = false"></u-picker>
			</view>

			<!-- 退款金额 -->
			<view class="section">
				<view class="section-title">退款金额</view>
				<view class="refund-amount">
					<text>¥</text>
					<u-input v-model="refundAmount" placeholder="请输入退款金额" type="number" border="none"></u-input>
					<text class="max-amount" @click="refundAmount = goodsInfo.price">最多¥{{goodsInfo.price}}</text>
				</view>
			</view>

			<!-- 问题描述 -->
			<view class="section">
				<view class="section-title">问题描述</view>
				<u--textarea v-model="problemDesc" placeholder="请详细描述您的问题，以便我们更好地为您服务" height="200" count
					maxlength="500" border="surround"></u--textarea>
			</view>

			<!-- 上传凭证 -->
			<view class="section">
				<view class="section-title">上传凭证<text class="sub-title">(最多9张)</text></view>
				<u-upload :fileList="evidenceImages" @afterRead="afterRead" @delete="deletePic" multiple maxCount="9"
					previewFullImage uploadText="上传凭证图片"></u-upload>
			</view>

			<!-- 提交按钮 -->
			<view class="submit-btn">
				<u-button type="error" shape="circle" text="提交申请" :loading="submitting"
					@click="submitRefund"></u-button>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		httpsend
	} from '@/api/api';
	export default {
		data() {
			return {
				id: null,
				scrollHeight: 0,
				goodsInfo: {}, //订单信息
				refundType: 1,
				refundTypes: [{
						label: '仅退款',
						value: 1
					},
					{
						label: '退货退款',
						value: 2
					}
				],
				showReasonPicker: false,
				refundReason: '七天无理由退款',

				reasonColumns: [
					[
						'七天无理由退款',
						'不想要了',
						'商品质量问题',
						'商品与描述不符',
						'收到商品破损',
						'其他原因'
					]
				],
				refundAmount: '',
				problemDesc: '',
				evidenceImages: [],
				submitting: false
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.calcScrollHeight();
			this.getdata();
		},
		methods: {
			async getdata() {
				try {
					let param = {
						id: this.id
					}
					uni.showLoading({
						title: '加载中...'
					});
					const res = await httpsend('getorderdetail', JSON.stringify(param));
					if (res?.code === 1) {
						//商品订单信息
						this.goodsInfo = res.data;
						this.refundAmount = this.goodsInfo['price'];
					} else {
						this.$api.msg(res.msg);
					}
					uni.hideLoading();

				} catch (err) {
					uni.hideLoading();
					console.log('err', err);
				}
			},
			calcScrollHeight() {
				const systemInfo = uni.getSystemInfoSync()
				this.scrollHeight = systemInfo.windowHeight
			},

			reasonConfirm(e) {
				this.refundReason = e.value[0].label;
				this.showReasonPicker = false;
			},

			// 图片上传
			afterRead(event) {
				// 实际项目中这里需要调用上传接口
				const file = event.file;
				this.evidenceImages.push({
					url: file.url
				});
			},

			// 删除图片
			deletePic(event) {
				this.evidenceImages.splice(event.index, 1);
			},

			// 提交退款申请
			async submitRefund() {
				if (!this.refundReason) {
					uni.showToast({
						title: '请选择退款原因',
						icon: 'none'
					});
					return;
				}

				if (!this.refundAmount) {
					uni.showToast({
						title: '请输入退款金额',
						icon: 'none'
					});
					return;
				}

				if (parseFloat(this.refundAmount) > parseFloat(this.goodsInfo.price)) {
					uni.showToast({
						title: '退款金额不能超过商品价格',
						icon: 'none'
					});
					return;
				}
				this.submitting = true;
				try {
					uni.showLoading({
						title: '正在处理...'
					});
					const res = await httpsend('applyaftersale');
					this.submitting = false;
					uni.hideLoading();
					this.$api.msg(res.msg);
					if (res?.code === 1) {
						uni.redirectTo({
							url: '/pages/order/orderlist?tab=4'
						});
					}
				} catch (err) {
					uni.hideLoading();
					console.log('err', err);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.refund-page {
		height: 100vh;
		background-color: #f7f7f7;
	}

	.form-container {
		padding: 20rpx;
	}

	.section {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;

		.section-title {
			font-size: 28rpx;
			font-weight: bold;
			margin-bottom: 20rpx;

			.sub-title {
				font-size: 24rpx;
				color: #999;
				font-weight: normal;
				margin-left: 10rpx;
			}
		}
	}

	.goods-card {
		display: flex;

		.goods-info {
			flex: 1;
			margin-left: 20rpx;

			.goods-name {
				font-size: 28rpx;
				color: #333;
				margin-bottom: 10rpx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.goods-spec {
				font-size: 24rpx;
				color: #999;
				margin-bottom: 10rpx;
			}

			.goods-price {
				font-size: 28rpx;
				color: #ff2d51;
				font-weight: bold;
			}
		}
	}

	.refund-amount {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #f5f5f5;

		text {
			font-size: 36rpx;
			color: #ff2d51;
			font-weight: bold;
		}

		.max-amount {
			font-size: 24rpx;
			color: #999;
			margin-left: auto;
		}
	}

	.submit-btn {
		margin: 40rpx 0;
	}

	/* uView 组件样式覆盖 */
	:deep(.u-radio-group) {
		width: 100%;
	}

	:deep(.u-radio) {
		margin-bottom: 20rpx;
	}

	:deep(.u-upload) {
		padding: 0;
	}
</style>